<template>
  <div class="my-container">
    <!-- 登录的头部 -->
    <div class="my-header user-info" v-if="user">
      <div class="base-info">
        <div class="left">
          <van-image :src="userinfo.photo" class="t-img" fit="cover" round />
          <span class="username">{{userinfo.name}}</span>
        </div>
        <div class="right">
          <van-button round size="mini" to="/user/profile">编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{userinfo.art_count}}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{userinfo.follow_count}}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{userinfo.fans_count}}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{userinfo.like_count}}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>
    <!--  未登录头部 -->
    <div class="my-header not-login" v-else>
      <div @click="$router.push('/login')" class="login-btn">
        <img alt src="@/assets/mobile.png" />
        <span class="text">登录 / 注册</span>
      </div>
    </div>
    <!-- 导航 -->
    <van-grid :column-num="2" class="grid-nav" clickable>
      <van-grid-item class="grid-item" icon="photo-o" text="文字">
        <i class="toutiao toutiao-shoucang" slot="icon"></i>
        <span class="text" slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item" icon="photo-o" text="文字">
        <i class="toutiao toutiao-lishi" slot="icon"></i>
        <span class="text" slot="text">历史</span>
      </van-grid-item>
    </van-grid>

    <van-cell is-link title="消息通知" />
    <van-cell class="mb-9" is-link title="小哎同学" />
    <van-cell @click="onLogout" class="logout-cell" clickable title="退出登录" v-if="user" />
  </div>
</template>

<script>
import { getUserInfo } from '../../api/user'
export default {
  name: 'MyIndex',
  data () {
    return {
      userinfo: {}
    }
  },
  computed: {
    user () {
      return this.$store.state.user
    }
  },
  created () {
    if (this.user) {
      this.loadUserInfo()
    }
  },
  methods: {
    onLogout () {
      this.$dialog.confirm({
        title: '确定要退出登录吗？',
      })
        .then(() => {
          // on confirm
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
          console.log('取消登录');
        })
    },
    async loadUserInfo () {
      try {
        const { data: res } = await getUserInfo()
        // console.log(res.data)
        this.userinfo = res.data
      } catch (err) {
        this.$toast('失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.my-container {
  .my-header {
    height: 361px;
    background: url("~@/assets/banner.png");
    background-size: cover;
  }
  .not-login {
    display: flex;
    // flex-direction: column;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      img {
        width: 132px;
        height: 132px;
      }
      .text {
        margin-top: 15px;
        color: #fff;
        font-size: 28px;
      }
    }
  }

  .user-info {
    .base-info {
      height: 231px;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .t-img {
          width: 132px;
          height: 132px;
          border: 4px solid #fff;
        }
        .username {
          margin-left: 25px;
          font-size: 38px;
          color: #fff;
        }
      }
    }
    .data-stats {
      display: flex;

      .data-item {
        height: 130px;
        flex: 1;
        color: #fff;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }
  .grid-nav {
    margin-bottom: 10px;
    .grid-item {
      height: 141px;
      i.toutiao {
        font-size: 45px;
      }
      .toutiao-shoucang {
        color: #eb5253;
      }
      .toutiao-lishi {
        color: #ff9d1d;
      }
      .text {
        font-size: 28px;
      }
    }
  }
  .mb-9 {
    margin-bottom: 9px;
  }
  .logout-cell {
    text-align: center;
    color: red;
  }
}
</style>